<!--
 * @version: 
 * @Company: 
 * @Description: 
 * @FilePath: \src\views\tool\gen\modal\create\index.vue
-->
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="创建表结构" :helpMessage="['填写创表语句']" width="700px" @cancel="sql = ''" :height="700" @ok="handleSubmit">
    <a-textarea v-model:value="sql" placeholder="输入创表语句" :auto-size="{ minRows: 24, maxRows: 24 }" showCount />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import * as _API from '../../api';

  export default defineComponent({
    components: { BasicModal },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const sql = ref<string>('');
      const { createMessage } = useMessage();
      const { warning } = createMessage;

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async () => {
        sql.value = '';
        setModalProps({
          confirmLoading: false,
        });
      });

      async function handleSubmit() {
        try {
          if (sql.value.length < 21) {
            warning('请输入正确的sql(创建表格语句)');
            return;
          }
          setModalProps({ confirmLoading: true });
          await _API.createAPI({ sql: sql.value }).then(() => {
            sql.value = '';
            closeModal();
            emit('success');
          });
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        sql,
        handleSubmit,
      };
    },
  });
</script>
